<script setup>
import { ref } from 'vue';

const props = defineProps({
    doLi: {
        type: Object,
        default: () => { }
    }
})
let edit = ref(false)
let del = ref(false)
let newText = ref('')
const emit = defineEmits(['deleteTodo', 'editTodo', 'toggerTodo'])
function deleteTodo() {
    emit('deleteTodo', props.doLi.id)
    del = false
}

function editTodo() {
    emit('editTodo', props.doLi.id,newText.value)
    edit = false
}
function toggerTodo() {
    emit('toggerTodo', props.doLi.id)
}

//方法名	详解
//ondraggable	设置元素是否允许被拖动。链接和图片默认是可拖动，因此不用设置该属性
//ondragstart	用户开始拖动元素或选择的文本时触发。
//ondragover	拖动元素或选取的文本正在拖动到放置目标时触发。
//默认情况下，数据/元素不能放置到其他元素中。
//如果要实现改功能，我们需要防止元素的默认处理方法。
//我们可以通过调用 event.preventDefault()方法来实现 ondragover 事件。
//ondragenter	当被鼠标拖动的对象进入其容器范围内时触发此事件
//ondragend	用户完成元素拖动后触发


</script>
<template>
    <div :class="doLi.done ? 'check list' : 'list' " @click="toggerTodo">
        <span>{{ doLi.text }}</span>
        <button class="edit" @click.stop="edit = true">修改</button>
        <button class="close" @click.stop="del = true">x</button>
    </div>
    <Teleport to="#app">
        <div v-if=edit class="edit-box">
            <div class="edit-box-content">
                <div class="old-text">原计划:{{ doLi.text }}</div>
                <input type="text" v-model="newText" placeholder="请输入新的计划">
                <br>
                <button class="save" @click="editTodo">保存</button>
                <button class="cancel" @click='edit = false'>取消</button>
            </div>

        </div>
    </Teleport>
    <Teleport to="#app">
        <div v-if=del class="del-box">
            <div class="edit-box-content">
                <div class="del-box-text">确认删除{{ doLi.text }}嘛?</div>
                <button class="save" @click="deleteTodo">确认</button>
                <button class="cancel" @click='del = false'>取消</button>
            </div>

        </div>
    </Teleport>
</template>
<style scoped>
   .list {
    list-style: none;
    position: relative;
    padding-left: 40px;
    height: 40px;
    line-height: 40px;
  }
  
  /* 将所有奇数列表项设置为不同的颜色（斑马线）*/
  .list:nth-child(odd) {
    background-color: #f9f9f9;
  }
  
  /* 光标移入的时候背景变暗 */
.list:hover {
    cursor: pointer;
    background-color: #dddddd;
  }
  
  /* 单击时，添加背景色并删除文本 */
.list.check {
    background-color: #888888;
    text-decoration: line-through;
    color: #f9f9f9;
  }
  
  /* 单击时添加“已选中”标记 */
 .list.check::before {
    content: '';
    position: absolute;
    border-color: #fff;
    border-style: solid;
    border-width: 0 2px 2px 0;
    top: 10px;
    left: 16px;
  
    transform: rotate(45deg);
    height: 15px;
    width: 7px;
  }
  
.edit {
    position: absolute;
    right: 60px;
    top: 3px;
    background-color: #4CAF50;
    color: white;
    border: none;
    padding: 5px 10px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    margin: 4px 2px;
    cursor: pointer;
}

.close {
    position: absolute;
    right: 20px;
    top: 5px;
    background-color: #f44336;
    color: white;
    border: none;
    padding: 5px 10px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    margin: 4px 2px;
    cursor: pointer;
}

.check {
    text-decoration: line-through;
    color: #aaa;

}

.edit-box {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
}

.edit-box-content {
    background-color: white;
    padding: 20px;
    border-radius: 5px;
}

.edit-box input {

    padding: 10px;
    border: none;
    border-radius: 5px;
    margin-right: 10px;
    font-size: 16px;
}

.save {
    background-color: #4CAF50;
    color: white;
    border: none;
    padding: 5px 10px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    margin: 4px 2px;
    cursor: pointer;
}

.cancel {
    background-color: #f44336;
    color: white;
    border: none;
    padding: 5px 10px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    margin: 4px 2px;
    cursor: pointer;
}

.del-box {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
}

.del-box-text {
    background-color: white;
    padding: 20px;
    border-radius: 5px;
    font-size: 18px;
    color: #f44336;
}
</style>
